$(function() {

	var partnerId = $('#partnerId').val();
	$('#partnerIdForm').val(partnerId); 
	
	var ordinals = new Array(
		'ORDINAL_FIRST'.translate(),
		'ORDINAL_SECOND'.translate(),
		'ORDINAL_THIRD'.translate(),
		'ORDINAL_FOURTH'.translate(),
		'ORDINAL_FIFTH'.translate(),
		'ORDINAL_SIXTH'.translate(),
		'ORDINAL_SEVENTH'.translate(),
		'ORDINAL_EIGHTH'.translate(),
		'ORDINAL_NINETH'.translate(),
		'ORDINAL_TENTH'.translate()
	);

	var agesMass = [];
	var agesMassMax = 0;
	$('#adults-list li:first select').each(function() {
		agesMass.push($(this).find('option').val());
	});
	agesMassMax = Math.max.apply(Math, agesMass);

	function showAdvancedSearch() {
		$('.search_controls a.more').hide();
		$('.search_controls a.less').css('display', 'block');
		$('.hotel_search_more').show();
		$('#adults-list').show();
	}

	var pVal = parseInt($('#tourists-number').text().replace(/(^\s+)|(\s+$)/g,""));
	$('#adults-list li select').attr('disabled', 'disabled');
	for (var i = 1; i <= pVal; i++) {
		var liNth = $('#adults-list li:nth-child(' + i + ')');
		liNth.find(' label').text(ordinals[i-1] + ' ' + 'TOURIST'.translate() + ':');
		liNth.find(' select').removeAttr('disabled');
		liNth.show();
		if (liNth.find('select').val() != agesMassMax) {
			showAdvancedSearch();
		}
	}

	if ($('#children').val() > 0 || $('#babies').val() > 0  || $('#rooms').val() > 1 || $('#price-from').val() != '' || $('#price-to').val() != '' || $('#nutrition-type').val() != '' || $('#input-hotelname').val() != '' || $('#chk-onlyavail').attr('checked') == true || $('#adults').val() != 2) {
		showAdvancedSearch();
	}

	$('#frm-hotel-search').submit(function() {
		if ($('#selector-country').val() == '' || $('#selector-city').val() == '') {
			alert('Нужно выбрать страну и город.');
			return false;
		} else {
			$('#hotel_search_outer, .error, .panel-error').css({position: 'absolute', top: '-999em'});
			$('#search_process').css({top: '0', position: 'relative'});
		}
	});


	if ($('.selector').val() != '') {
		var s = $('.selector');
		var m = s.data();
		doSelectorRequest($('.selector'), $('.selector').data());
	}

	function doSelectorRequest(s, m) {
		var t = $(m.target), p = {id: s.val(), show_all_cities: 1};
		if (m.params !== undefined) {
			for (var param in m.params) {
				p[param] = m.params[param];
			}
		}
		t.html('<option value="">Загрузка...</option>');
		t.attr('disabled', 'disabled');
		$.get(m.url, p, function (data) {
			t.attr('disabled', '');
			var h = '';
			h += '<option value="' + m.defValue + '" selected="selected" class="first">' + m.defText + '</option>';
			t.html(h + data);
			t.val(m.defValue);
			setTimeout(function() {
				$('#selector-city option[value="' + $('#city-value').val() + '"]').attr('selected', 'selected');
			}, 0);
		});
	}
	
	$('.selector').change(function() {
		var s = $(this); var m = s.data();
		if (m.targets !== undefined) {
			for (var i = 0, l = m.targets.length; i < l; i++) {
				doSelectorRequest(s, m.targets[i]);
			};
		} else {
			doSelectorRequest(s, m);
		}
	});
	
	$('#adults').change(function() {
		if ($(this).val() != 2) {
			$('#adults-list').show();
			$('.hotel_search_more').show();
			$('.more-less-controls .more').hide();
			$('.more-less-controls .less').css('display', 'block');
		}
		var $this = $(this);
		$('#adults-list li').hide();
		$('#adults-list li select').attr('disabled', 'disabled');
		for (var i = 1; i <= $this.val(); i++) {
			$('#adults-list li:nth-child(' + i + ') select').removeAttr('disabled');
			$('#adults-list li:nth-child(' + i + ') label').text(ordinals[i-1] + ' ' + 'TOURIST'.translate() + ':');
			$('#adults-list li:nth-child(' + i + ')').show();
		}
	});

});